{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# List\n",
    "my_list = [1, 2, 3]\n",
    "iterator = iter(my_list)\n",
    "\n",
    "print(next(iterator))  # Output: 1\n",
    "print(next(iterator))  # Output: 2\n",
    "print(next(iterator))  # Output: 3\n",
    "\n",
    "# Raises StopIteration when no more elements are left\n",
    "try:\n",
    "    print(next(iterator))\n",
    "except StopIteration:\n",
    "    print(\"End of iterator\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "string = \"hello\"\n",
    "iterator = iter(string)\n",
    "\n",
    "print(next(iterator))  # Output: h\n",
    "print(next(iterator))  # Output: e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You entered: stop\n"
     ]
    }
   ],
   "source": [
    "def read_input():\n",
    "    return input(\"Enter something: \")\n",
    "\n",
    "\n",
    "# Iterator stops when \"STOP\" is entered\n",
    "iterator = iter(read_input, \"STOP\")\n",
    "\n",
    "for value in iterator:\n",
    "    print(f\"You entered: {value}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Counter:\n",
    "    def __init__(self, start: int, end: int):\n",
    "        self.current = start\n",
    "        self.end = end\n",
    "\n",
    "    def __iter__(self):\n",
    "        return self  # Returns itself as the iterator\n",
    "\n",
    "    def __next__(self):\n",
    "        if self.current > self.end:\n",
    "            raise StopIteration\n",
    "        self.current += 1\n",
    "        return self.current - 1\n",
    "\n",
    "\n",
    "# Create a Counter iterator\n",
    "counter = Counter(1, 5)\n",
    "for number in counter:\n",
    "    print(number)\n",
    "# Output: 1 2 3 4 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "def generator():\n",
    "    for i in range(3):\n",
    "        yield i\n",
    "\n",
    "\n",
    "gen = generator()\n",
    "iterator = iter(gen)\n",
    "\n",
    "print(next(iterator))  # Output: 0\n",
    "print(next(iterator))  # Output: 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a story\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open(\"example.txt\") as file:\n",
    "    for line in iter(file.readline, \"#\"):\n",
    "        print(line.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import partial\n",
    "\n",
    "with open(\"example.txt\", \"rb\") as f:\n",
    "    chunk_size = 1024\n",
    "    for chunk in iter(partial(f.read, chunk_size), b\"\"):\n",
    "        print(chunk)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
